package main

import (
	// "fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"os"
)

type User struct {
	Name  string
	Count int
}

var num int = 0

type myHandler struct{}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "URL:"+r.URL.String())
}
func HelloServer(w http.ResponseWriter, req *http.Request) {
	requestParam := req.URL.Path[1:]
	num++
	// fmt.Printf("Hello, %s,%d\n", requestParam, num)
	t, err := template.ParseFiles("../static/index.html")
	if err != nil {
		log.Println(err)
	}

	t.Execute(w, &User{requestParam, num})
}

func main() {
	//设置路由
	mux := http.NewServeMux()
	mux.Handle("/", &myHandler{})
	mux.HandleFunc("/hello", HelloServer)

	_, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	//静态文件服务
	mux.Handle("/js/",
		http.StripPrefix("/js/",
			http.FileServer(http.Dir("D:\\golang\\Workspaces\\src\\lean\\static\\js"))))
	mux.Handle("/css/",
		http.StripPrefix("/css/",
			http.FileServer(http.Dir("D:\\golang\\Workspaces\\src\\lean\\static\\css"))))
	mux.Handle("/images/",
		http.StripPrefix("/images/",
			http.FileServer(http.Dir("D:\\golang\\Workspaces\\src\\lean\\static\\images"))))

	// fmt.Printf("%v\n", http.StripPrefix("/js/", http.FileServer(http.Dir(wd))))
	err = http.ListenAndServe(":8081", mux)
	if err != nil {
		log.Fatal(err)
	}
}
